package com.yjd.bi.statistic.control;

import com.yjd.bi.comm.utils.BiDateUtil;
import com.yjd.bi.report.enums.TimeType;
import com.yjd.bi.report.model.EChartsModel;
import com.yjd.bi.user.service.ILoginFrequencyService;
import com.yjd.comm.annotation.DateString;
import com.yjd.comm.annotation.ServiceRemoteProxy;
import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

@Controller
@Scope("prototype")
@RequestMapping("/loginfrequency")
public class LoginFrequencyControl extends BaseControl {

    @ServiceRemoteProxy(serviceUrlResouceKey = RpmServiceKeyEnum.RPM_BI_SERVICE_KEY)
    private ILoginFrequencyService loginFrequencyService;


    /**
     * 登录频率统计页面
     *
     * @return
     * @throws Exception String
     */
    @RequestMapping("index")
    public String index() throws Exception {
        request.setAttribute("start", "2013-12-21");
        request.setAttribute("end", BiDateUtil.initBiDate());
        return "loginfrequency.index";
    }

    /**
     * 登录频率统计
     *
     * @throws Exception void
     */
    @RequestMapping("loginacount")
    public void loginCount(@RequestParam(required = false) Integer timeType,
                           @DateString String timeFrom,
                           @RequestParam(required = false) String field) throws Exception {
        EChartsModel echartsMoel = loginFrequencyService.getDates(
                TimeType.getType(timeType), Long.valueOf(timeFrom), field);
        super.successData(FrameUtil.newHashMap("option", echartsMoel));
    }

    /**
     * 注册之日起30日内只登录过1次用户
     *
     * @param start
     * @param end
     * @throws Exception
     */
    @RequestMapping("/loginCountof30")
    public void loginCountof30(@DateString String start, @DateString String end) throws Exception {
        Map resultMap = new HashMap();
        resultMap.put("loginCount30", loginFrequencyService.getLoginCountOf30(FrameUtil.newHashMap("startTime",  FrameUtil.getTime(DateUtil.parseDate(start,
                DateUtil.PATTERN_yyyyMMdd)), "endTime", FrameUtil.getTime(DateUtil.parseDate(end,
                DateUtil.PATTERN_yyyyMMdd))+86399)));
        super.successData(resultMap);
    }
}
